package servlet;

import Data.data;
import com.sun.xml.internal.ws.util.xml.CDATA;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
import org.apache.hadoop.hbase.util.Bytes;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/inquire")
public class inquire extends HttpServlet {



    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int i;
        ArrayList<data> dataList = new ArrayList<>();
        String getid = request.getParameter("id");
        String getname = request.getParameter("name");
        //  创建HBase配置对象
        Configuration configuration = HBaseConfiguration.create();
        // 指定zookeeper集群地址
        configuration.set("hbase.zookeeper.quorum", "192.168.116.11");
        // 创建连接对象
        Connection connection = ConnectionFactory.createConnection(configuration);
        // 获取Table对象,Table负责与记录相关的操作,如增删改查等
        Table table = connection.getTable(TableName.valueOf("goodsmessage"));

//        for (i=1; i<=10; i++) {
//         Get get = new Get(String.valueOf(i).getBytes());
//                Get get = new Get(Bytes.toBytes(i));
//                Result result = table.get(get);
//
//            Cell[] cells = result.rawCells();
//            String goodsid = new String(CellUtil.cloneValue(cells[0]));
//            String goodsname =  new String(CellUtil.cloneValue(cells[1]));
//            String goodsprice = new String(CellUtil.cloneValue(cells[2]));
//            String goodstime =  new String(CellUtil.cloneValue(cells[3]));
//            String goodsstatus =  new String(CellUtil.cloneValue(cells[4]));
//            data goodsmessage = new data();
//            goodsmessage.setGoodsid(goodsid);
//
//            goodsmessage.setGoodsname(goodsname);
//            goodsmessage.setGoodsprice(goodsprice);
//            goodsmessage.setGoodstime(goodstime);
//            goodsmessage.setGoodsstatus(goodsstatus);
//            dataList.add(goodsmessage);
        Scan scan = new Scan();
        ResultScanner result = table.getScanner(scan);

        for (Result rs : result) {
            String a = null;
            String b = null;
            String c = null;
            String d = null;
            String e = null;
            for (KeyValue kv : rs.raw()) {

                if (new String(kv.getQualifier()).equals("id")) {
                    a = new String(kv.getValue());

                } else if (new String(kv.getQualifier()).equals("name")) {
                    b = new String(kv.getValue());
                }
                else if (new String(kv.getQualifier()).equals("price")) {
                    c = new String(kv.getValue());
                }
                    else if (new String(kv.getQualifier()).equals("time")) {
                    d = new String(kv.getValue());
                }
                else if (new String(kv.getQualifier()).equals("status")) {
                    e = new String(kv.getValue());
                }



//                else
//                {
//
//                }

            }
            if (getid.equals(a)||getname.equals(b)) {
                data goodsmessage = new data();
                goodsmessage.setGoodsid(a);
                goodsmessage.setGoodsname(b);
                goodsmessage.setGoodsprice(c);
                goodsmessage.setGoodstime(d);
                goodsmessage.setGoodsstatus(e);
                dataList.add(goodsmessage);
                request.setAttribute("goodmessage", dataList);
                request.getRequestDispatcher("/query9.jsp").forward(request, response);
            }


//            if (getid.equals()||getname.equals())
//            {
//                request.setAttribute("dataList",dataList);
//                request.getRequestDispatcher("/inquire.jsp").forward(request,response);
//            }
//            else
//                request.setAttribute("error","未查询到该订单");
        }
        request.setAttribute("error","未查询到该订单");
        request.getRequestDispatcher("/query9.jsp").forward(request, response);
    }



//    public Integer rowCount(String tablename){
//        Integer rowCount = 0;
//        try {
//            //  创建HBase配置对象
//            Configuration configuration = HBaseConfiguration.create();
//            // 指定zookeeper集群地址
//            configuration.set("hbase.zookeeper.quorum", "192.168.116.11");
//            // 创建连接对象
//            Connection connection = ConnectionFactory.createConnection(configuration);
//            // 获取Table对象,Table负责与记录相关的操作,如增删改查等
//            Table table = connection.getTable(TableName.valueOf("goodsmessage"));
//            Scan scan = new Scan();
//            //FirstKeyOnlyFilter只会取得每行数据的第一个kv，提升count速度
//            scan.setFilter(new FirstKeyOnlyFilter());
//
//            ResultScanner rs = table.getScanner(scan);
//            for (Result result : rs) {
//                rowCount ++;
//            }
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//        return rowCount;
//    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
